package lib.bone.admin.controller.flow;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.flow.domain.dto.AttachQueryDTO;
import lib.bone.flow.domain.entity.FlowAttach;
import lib.bone.flow.service.FlowAttachService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("flow")
@Api(tags = "流程流转信息")
public class FlowAttachController {
    @Autowired
    FlowAttachService service;
    
    @GetMapping("attach")
    @ApiOperation(value = "流转信息列表查询")
    public ResponseDTO getList(AttachQueryDTO dto){
        Sort sort;
        if(dto.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, dto.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, dto.getSortBy());
        }
        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(dto.getPageNum() - 1 , dto.getPageSize(), sort);

        FlowAttach attach = new FlowAttach();
        BeanUtil.copyProperties(dto,attach, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FlowAttach> example = Example.of(attach);

        return service.getList(pageable,example);
    }
}
